package com.smedi.ismedi.organization.core.port.adapter.service.impl;

import com.smedi.ismedi.constructionproject.api.dto.request.ProjectAssignmentListQueryRequest;
import com.smedi.ismedi.constructionproject.api.dto.request.ProjectListQueryRequest;
import com.smedi.ismedi.constructionproject.api.dto.response.ProjectBasicResponse;
import com.smedi.ismedi.constructionproject.api.dto.response.resp.ProjectAssignmentResp;
import com.smedi.ismedi.constructionproject.api.dto.response.resp.ProjectResp;
import com.smedi.ismedi.constructionproject.api.feign.RemoteBidSectionFeignApi;
import com.smedi.ismedi.constructionproject.api.feign.RemoteConstructionProjectFeignApi;
import com.smedi.ismedi.constructionproject.api.feign.RemoteProjectAssignmentFeignApi;

import com.smedi.ismedi.organization.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.organization.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.organization.core.port.adapter.persistence.dto.ProjectAssignmentQuery;
import com.smedi.ismedi.organization.core.port.adapter.persistence.dto.ProjectQuery;
import com.smedi.ismedi.organization.core.port.adapter.service.ConstructionProjectRemoteApiService;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-6-24 10:57
 */
@Slf4j
@Service
@AllArgsConstructor
public class ConstructionProjectRemoteApiServiceImpl implements ConstructionProjectRemoteApiService {
    private final RemoteConstructionProjectFeignApi remoteConstructionProjectFeignApi;

    private final RemoteProjectAssignmentFeignApi remoteProjectAssignmentFeignApi;

    private final RemoteBidSectionFeignApi remoteBidSectionFeignApi;

    private final AdapterConverter adapterConverter;

    /**
     * {@inheritDoc}
     */
    @Override
    public Boolean checkHrOrganizationHasProject(Long hrOrganizationId) {
        ResponseData<Boolean> response = remoteConstructionProjectFeignApi.getCheckHrOrganizationHasProject(hrOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Boolean checkProjectAvailable(Long projectId) {
        ResponseData<Boolean> response = remoteConstructionProjectFeignApi.getCheckProjectAvailable(projectId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Boolean checkProjectOrganizationBeUsedInBiSection(Long projectOrganizationId) {
        ResponseData<Boolean> response = remoteBidSectionFeignApi.getCheckProjectOrganizationBeUsedInBidSection(projectOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Boolean validateProjectOrganizationHasProjectAssignment(Long projectOrganizationId) {
        ResponseData<Boolean> response = remoteProjectAssignmentFeignApi.getValidateProjectOrganizationHasAssignment(projectOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public ProjectBasicResponse getProjectBasicView(Long projectId) {
        ResponseData<ProjectBasicResponse> response = remoteConstructionProjectFeignApi.getQueryProjectBasicView(projectId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public List<ProjectResp> getProjectListWithoutToken(ProjectQuery projectQuery) {
        ProjectListQueryRequest projectListQueryRequest = adapterConverter.projectQueryToProjectListQueryRequest(projectQuery);
        ResponseData<List<ProjectResp>> response = remoteConstructionProjectFeignApi.getProjectListWithoutToken(projectListQueryRequest);
        if (!response.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        } else {
            return response.getData();
        }
    }

    @Override
    public List<ProjectAssignmentResp> getProjectAssignmentList(ProjectAssignmentQuery projectAssignmentQuery) {
        ProjectAssignmentListQueryRequest projectAssignmentListQueryRequest = adapterConverter.projectAssignmentQueryToProjectAssignmentListQueryRequest(projectAssignmentQuery);
        ResponseData<List<ProjectAssignmentResp>> response = remoteProjectAssignmentFeignApi.getProjectAssignmentList(projectAssignmentListQueryRequest);
        if (!response.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        } else {
            return response.getData();
        }
    }
}
